package com.atguigu.javaailangchain4j.config;

import com.atguigu.javaailangchain4j.stroy.MongoChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class SeparateChatAssistantConfig {
    //注入持久化对象
    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Autowired
    EmbeddingModel embeddingModel;

    @Autowired
    EmbeddingStore embeddingStore;

    /**
     * 配置Pincone
     * @return
     */
    @Bean
    ContentRetriever contentRetrieverPincone(){
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore) // 配置阿里云百炼储存
                .embeddingModel(embeddingModel) // 配置嵌入模型
                .maxResults(1) // 只返回一条
//                .minScore(0.8)
                .build();
//         embeddingStoreContentRetriever;
    }


    /**
     * 每次都读取本地文件作为知识库
     * @return 将读取到的进行返回
     */
    @Bean
    ContentRetriever contentRetrieverReadDocument() {
        // 读取文档信息
        List<Document> documents = FileSystemDocumentLoader.loadDocuments("D:/nwt文件记录/cache/recv/徐老师/0715课件/小智医疗项目-xu/资料/04-科室信息");

        // 初始化向量储存内容
        InMemoryEmbeddingStore<TextSegment> inMemoryEmbeddingStore =new InMemoryEmbeddingStore<>();

        // 向量转换
        EmbeddingStoreIngestor.ingest(documents,inMemoryEmbeddingStore);

        return EmbeddingStoreContentRetriever.from(inMemoryEmbeddingStore);
    }
    
    @Bean
    ChatMemoryProvider chatMemoryProvider() {
        return memoryId -> MessageWindowChatMemory.builder()
        .id(memoryId)
        .maxMessages(10)
        .chatMemoryStore(mongoChatMemoryStore)//配置持久化对象
        .build();
    }
}